﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Neuron.Cell;
using Neuron.Cell.Control;

namespace Neuron.NetworkDesigner
{
    public partial class ctrlBaseNetwork : UserControl
    {
        PerceptronBackbone _backbone = null;


        private ctrlPerceptronDrawingBoard _drawingBoard = null;

        public ctrlBaseNetwork()
        {
            InitializeComponent();
        }

        public void setBackbone(PerceptronBackbone backbone)
        {
            this._backbone = backbone;
        }

        public ctrlPerceptronDrawingBoard DrawingBoard
        {
            set { this._drawingBoard = value; }
        }

        private void buttonReceptronsUP_Click(object sender, EventArgs e)
        {
            int number = Convert.ToInt32(textBoxReceptrons.Text);
            number += 1;
            textBoxReceptrons.Text = Convert.ToString(number);
        }

        private void buttonReceptronsDOWN_Click(object sender, EventArgs e)
        {
            int number = Convert.ToInt32(textBoxReceptrons.Text);
            if (number > 0) number -= 1;
            textBoxReceptrons.Text = Convert.ToString(number);
        }

        private void buttonActronsUP_Click(object sender, EventArgs e)
        {
        }

        private void buttonActronsDOWN_Click(object sender, EventArgs e)
        {
        }

        private void buttonCreateEnvironment_Click(object sender, EventArgs e)
        {
            if (this.checkBoxDeleteExistingNet.Checked == true)
            {
                this._drawingBoard.clearDrawingBoard(sender, e);
            }

            if (this._drawingBoard.NumberOfPerceptrons == 0)
            {
                int maxRec = Convert.ToInt32(textBoxReceptrons.Text);

                int maxCol = maxRec;

                Dictionary<string, Perceptron> dictPerceptrons = new Dictionary<string, Perceptron>();

                Perceptron newPerceptron = null;
                for (int i = 0; i < maxRec * 2; i++)
                {
                    if ((i % 2) == 0)
                    {
                        newPerceptron = new Perceptron(this._backbone, "P.997." + Convert.ToString(i), 1.0, ActivationFunctionEnum.hlim, OnNeuronEnum.withoutOnNeuron);
                    } else
                    {
                        newPerceptron = new Perceptron(this._backbone, "P.997." + Convert.ToString(i), 1.0, ActivationFunctionEnum.hlim, OnNeuronEnum.withOnNeuron);
                    }
                    this._drawingBoard.createPerceptronControlAt(10 + (i * 95), 150, newPerceptron);
                    //dictPerceptrons.Add(newPerceptron.Name, newPerceptron);
                }

                for (int i = 0; i < maxRec * 2; i+=2)
                {
                    Perceptron newRec = new Perceptron(this._backbone, "P.1." + Convert.ToString(i), 1.0, ActivationFunctionEnum.receptor, OnNeuronEnum.withoutOnNeuron);
                    int index = i / 2;
                    for (int j = 0; j < maxRec * 2; j++)
                    {
                        this._backbone.getPerceptronInstanceByName("P.997." + Convert.ToString(j)).addInputChannel(newRec);
                    }
                    this._drawingBoard.createPerceptronControlAt(57 + (index * 190), 10, newRec);
                }

                for (int i = 0; i < maxRec * 2; i += 2)
                {
                    newPerceptron = new Perceptron(this._backbone, "P.998." + Convert.ToString(i), 1.0, ActivationFunctionEnum.hlim, OnNeuronEnum.withoutOnNeuron);
                    int index = i / 2;
                    newPerceptron.addInputChannel(this._backbone.getPerceptronInstanceByName("P.997." + Convert.ToString(i)));
                    newPerceptron.addInputChannel(this._backbone.getPerceptronInstanceByName("P.997." + Convert.ToString(i + 1)));
                    this._drawingBoard.createPerceptronControlAt(57 + (index * 190), 290, newPerceptron);
                }

                for (int i = 0; i < maxRec * 2; i+=2)
                {
                    Perceptron newAct = new Perceptron(this._backbone, "P.999." + Convert.ToString(i), 0.5, ActivationFunctionEnum.actor, OnNeuronEnum.withoutOnNeuron);
                    int index = i / 2;
                    newAct.addInputChannel(this._backbone.getPerceptronInstanceByName("P.998." + Convert.ToString(i)));
                    this._drawingBoard.createPerceptronControlAt(57 + (index * 190), 430, newAct);
                }

                this._drawingBoard.RedrawConnections();
            }
        }
    }
}
